package fun.sboy.panel;

import fun.sboy.pojo.ManagerUser;
import fun.sboy.util.IconUtil;
import fun.sboy.util.JDBCUtil;
import fun.sboy.view.HomeView;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;

public class InfoPanel extends BaseJPanel {

    private JLabel username = new JLabel("持卡人  ：", new ImageIcon(IconUtil.loadSmall("user")), JLabel.LEFT);
    private JLabel sex = new JLabel("性别    ：", new ImageIcon(IconUtil.loadSmall("partner")), JLabel.LEFT);
    private JLabel moneyBalance = new JLabel("剩余金额：", new ImageIcon(IconUtil.loadSmall("renew")), JLabel.LEFT);
    private JLabel phone = new JLabel("电话号码：", new ImageIcon(IconUtil.loadSmall("phonecall")), JLabel.LEFT);
    private JLabel address = new JLabel("居住地址：", new ImageIcon(IconUtil.loadSmall("applets")), JLabel.LEFT);
    private JLabel cardNumber = new JLabel("卡号    ：", new ImageIcon(IconUtil.loadSmall("international")), JLabel.LEFT);

    //面板信息，会经过数据库的刷新
    private JLabel dUsername = new JLabel("chen");
    private JLabel dSex = new JLabel("sex");
    private JLabel dMoneyBalance = new JLabel("moneyBalance");
    private JLabel dPhone = new JLabel("Phone");
    private JLabel dAddress = new JLabel("Address");
    private JLabel dCardNumber = new JLabel("CardNumber21312332513567891");


    public InfoPanel(LayoutManager layout, boolean isDoubleBuffered) {
        super(layout, isDoubleBuffered);
        initialization();
    }

    public InfoPanel(LayoutManager layout) {
        super(layout);
        initialization();
    }

    public InfoPanel(boolean isDoubleBuffered) {
        super(isDoubleBuffered);
        initialization();
    }

    public InfoPanel() {
        initialization();
    }

    @Override
    public void initialization() {
        ArrayList<JLabel> list = new ArrayList<>();
        list.add(username);
        list.add(sex);
        list.add(moneyBalance);
        list.add(phone);
        list.add(address);
        list.add(cardNumber);

        int x = 40;
        int y = 40;
        for (int i = 0; i < list.size(); i++) {
            JLabel j = list.get(i);
            if (i % 2 == 0) {
                j.setBounds(x, y, 200, 30);
                add(j);
                x += 400;
            } else {
                j.setBounds(x, y, 200, 30);
                add(j);
                y += 60;
                x = 40;
            }
        }
        reload();
        list.clear();
        list.add(dUsername);
        list.add(dSex);
        list.add(dMoneyBalance);
        list.add(dPhone);
        list.add(dAddress);
        list.add(dCardNumber);
        x = 150;
        y = 40;
        for (int i = 0; i < list.size(); i++) {
            JLabel j = list.get(i);
            j.setBounds(x, y, 400, 30);
            add(j);
            if (i % 2 == 0) {
                x += 400;
            } else {
                y += 60;
                x = 150;
            }
        }

    }


    /**
     * 加载视图数据
     */
    @Override
    public void reload() {

        Integer id = HomeView.user.getId();
        String sql = "SELECT * FROM manager_user WHERE id = " + id;
        ManagerUser managerUser = JDBCUtil.selectOne(sql, ManagerUser.class);

        dUsername.setText(managerUser.getName());
        String sex = managerUser.getSex().equals("1") ? "男" : "女";
        dSex.setText(sex);
        dMoneyBalance.setText(String.valueOf(managerUser.getMoney()));
        dPhone.setText(managerUser.getPhone());
        dAddress.setText(managerUser.getAddress());
        dCardNumber.setText(managerUser.getCardNumber());

    }
}